package com.ruihang.generalibrary.utils;

import android.os.SystemClock;
import android.util.Log;

import java.util.Date;

/**
 * Created by Administrator on 2017/5/8.
 */

public class ServerTime {
    private long elaTime;
    private long serverTime;
    private static ServerTime instance;
    private static int HOUR_24 = 48 * 60 * 60 * 1000;

    public static ServerTime getInstance(long serverTime) {
        if (instance == null) {
            instance = new ServerTime(serverTime);
        } else {
            if (instance.serverTime <= 0) {
                instance.serverTime = serverTime;
            }
        }
        return instance;
    }

    private ServerTime(long serverTIme) {
        this.serverTime = serverTIme;
//        this.serverTime = DateUtils.paserStringDate("2017-05-11 20:47:00", DateUtils.PATTERN_IN);
        elaTime = SystemClock.elapsedRealtime();
        Log.e("ServerTime", "elaTime:" + DateUtils.formatDate(new Date(elaTime), DateUtils.PATTERN_IN));
    }

    public long getServerRealTimeMilByEla() {
        if (serverTime > 0) {
            return serverTime + (SystemClock.elapsedRealtime() - elaTime);
        } else {
            return System.currentTimeMillis();
        }
    }
}
